Skip to content

Commit

Permalink
Disallow __proto__ assignment
Browse files Browse the repository at this point in the history
Disallow assignment to the prototype using `set()`.

This resolves a Prototype Pollution vulnerability. An attacker could
have assigned a value to the target object's prototype, which can modify
how objects behave throughout the entire application.
  • Loading branch information
isaymatato committed Nov 23, 2020
1 parent 787ada7 commit 24935e6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
4 changes: 4 additions & 0 deletions index.js
Expand Up @@ -55,6 +55,10 @@ Reference.createFromPath = function(pointer, path) {
var reference = new Reference(pointer);
path = path.split('.');
path.forEach(function(field) {
if (field === '__proto__') {
throw new Error('Cannot assign reference to prototype')
}

var isNumber = checkIfStringIsNumber(field);
var hasParent = reference.getParent() ? true : false;
if (isNumber) {
Expand Down
10 changes: 9 additions & 1 deletion test/index.js
Expand Up @@ -42,6 +42,14 @@ describe('#set', function() {
obj.a[1].should.equal('B');
obj.a[2].should.equal('C');
});

context('attempt to modify prototype', function() {
it('throws an error', function() {
assert.throws(
() => set({}, '__proto__.foo', 'hacker_attack!'),
Error, 'Cannot assign reference to prototype')
})
})
});

describe('#decorate', function() {
Expand All @@ -67,4 +75,4 @@ describe('#undecorate', function() {
undecorate(obj);
expect(obj.setKey).to.not.be.a('function');
});
});
});

1 comment on commit 24935e6

@abergmann
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CVE-2020-28274 was referenced to this commit.

Please sign in to comment.